﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using UltimusEvents;
using Ultimus.WFServer;

namespace Subscriber
{
    public class Subscriber : System.EnterpriseServices.ServicedComponent, IUltimusPublisher
    {
        clsOleDB clsDb = new clsOleDB();
        public Subscriber()
        {

            clsDb.setConn("oleDB");
        }
        public void ActiveTaskDeleted(string strProcessName, int nIncident, string strTaskId)
        {
            // throw new NotImplementedException();
        }

        public void ActiveUsersViolation(int ViolationNumber, string[] Users)
        {
            // throw new NotImplementedException();
        }

        public void CompletedTaskDeleted(string strProcessName, int nIncident, string strTaskId)
        {
            // throw new NotImplementedException();
        }

        public void IncidentAborted(string strProcessName, int nIncident, string strReason)
        {
            try
            {
                string str = UltAction.UpdateStatus(strProcessName, nIncident, 4, clsDb);
                clsDb.wteLog("", false, "流程终止事件:" + str);
            }
            catch (Exception ex)
            {
                clsDb.wteLog("", false, "流程终止事件出错:" + ex.Message);
            }
        }

        public void IncidentCompleted(string strProcessName, int nIncident)
        {
            try
            {
                string str=UltAction.UpdateStatus(strProcessName,nIncident, 2,clsDb);
                clsDb.wteLog("", false, "流程完成事件:" + str);
            }
            catch (Exception ex)
            {
                clsDb.wteLog("", false, "流程完成事件出错:" + ex.Message);
            }
        }

        public void IncidentInitiated(string strProcessName, int nIncident)
        {
            //   throw new NotImplementedException();
        }

        public void QueueTaskActivated(string strProcessName, int nIncident, string strTaskId)
        {
            // throw new NotImplementedException();
        }

        public void TaskAborted(string strProcessName, int nIncident, string strTaskId)
        {
            //  throw new NotImplementedException();
        }

        public void TaskActivated(string strProcessName, int nIncident, string strTaskId)
        {
            //throw new NotImplementedException();
            try
            {
                clsDb.wteLog("", false, "初始化Task对象[" + strTaskId +"]");
                //初始化Task对象
                Task tsk = new Task();
                tsk.InitializeFromTaskId(strTaskId);
                clsDb.wteLog("", false, "将子流程转成主流程");
                //将子流程转成主流程
                //string processname = AutolivAction.ReplaceProcessName(tsk.strProcessName);
                //Incident inc = new Incident();
                string processname = tsk.strProcessName;
               // int nIncident = tsk.nIncidentNo;
                string strMainTaskid = UltAction.GetMainTaskid(strProcessName, nIncident, clsDb);
                clsDb.wteLog("", false, "初始化话主任务TaskId" + strMainTaskid);
                //初始化话主任务TaskId
                if (strMainTaskid != null && strMainTaskid.Trim() != "")
                {
                    Task mainTask = new Task();
                    mainTask.InitializeFromTaskId(strMainTaskid);
                    processname = mainTask.strProcessName;
                    nIncident = mainTask.nIncidentNo;
                }
               // clsDb.wteLog("", false, "当前任务处理人" + tsk.strUser);
                //当前任务处理人
                string tskUser = tsk.strUser;
                //查找是否有指派
                bool isExistAssign = false;
                
                //当前任务被指派人
                string tskAssignUser = "";
                isExistAssign = UltAction.isExistAssign(processname, tskUser, ref tskAssignUser, clsDb);
               // clsDb.wteLog("", false, "当前任务被指派人" + tskAssignUser);
                //指派任务
                bool isAssigned = false;
                //判断是否存在指派 并且被指派人不为空
                if (isExistAssign && !string.IsNullOrEmpty(tskAssignUser))
                {

                    isAssigned = tsk.AssignTask(tskAssignUser);
                }
               // clsDb.wteLog("", false, "指派成功" + isAssigned);
                //指派成功
                if (isAssigned)
                {
                    //指派记录对象
                    AssignRecordModel arm = new AssignRecordModel();
                    arm.ID = Guid.NewGuid().ToString();
                    arm.ProcessName = tsk.strProcessName;
                    arm.Incident = tsk.nIncidentNo;
                    arm.Taskid = tsk.strTaskId;
                    arm.TskUser = tskUser;
                    arm.TskAssignUser = tskAssignUser;
                    arm.TskAssignDate = DateTime.Now;
                    // clsDb.wteLog("", false, "添加指派记录" + arm.ID);
                    //添加指派记录
                    AssignRecordLogic.Add(arm, clsDb);

                    //    clsDb.wteLog("", false, "更新Autoliv ApprovalHistory" );
                    
                }
                else
                {
                    clsDb.wteLog("",false,"指派失败:" + strMainTaskid);
                }
            }
            catch (Exception ex)
            {
                clsDb.wteLog("", false, "任务激活事件出错:" + ex.Message);
            }
        }

        public void TaskAssigned(string strProcessName, int nIncident, string strTaskId, string strAssignedUser)
        {
            // throw new NotImplementedException();
        }

        public void TaskCompleted(string strProcessName, int nIncident, string strTaskId)
        {
            // throw new NotImplementedException();
        }

        public void TaskConferred(string strProcessName, int nIncident, string strTaskId, string strUser)
        {
            //  throw new NotImplementedException();
        }

        public void TaskDelayed(string strProcessName, int nIncident, string strTaskId)
        {
            // throw new NotImplementedException();
        }

        public void TaskOverdue(string strProcessName, int nIncident, string strTaskId)
        {
            // throw new NotImplementedException();
        }

        public void TaskResubmitted(string strProcessName, int nIncident, string strTaskId)
        {
            //  throw new NotImplementedException();
        }

        public void TaskReturned(string strProcessName, int nIncident, string strTaskId)
        {
            //  throw new NotImplementedException();
        }

        public void TaskSubmitFailed(string strTaskId)
        {
            //  throw new NotImplementedException();
        }

        public void TransactionsViolation(int ViolationNumber, int TransactionPeak)
        {
            // throw new NotImplementedException();
        }
    }
}
